<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace app\api\controller\haoyayi;
use think\Controller as ThinkController;

class Login extends ThinkController {

    public function index() {
        
        $trd_session_key = $_GET['trd_session_key']; //
        $code = $_GET['code']; //
        $w_name = $_GET['w_name']; //
        $w_gender = $_GET['w_gender']; //
        $w_city = $_GET['w_city']; //
        $w_province = $_GET['w_province']; //
        $w_country = $_GET['w_country']; //
        $w_avatarUrl = $_GET['w_avatarUrl']; //

        $appId = "wx9c1d9fe72cf0f2e7";
        $appSecret = "a12646baf9a2248824dcef589c79178e";
        $url = "https://api.weixin.qq.com/sns/jscode2session?";

        if (isset($code)) {
            $url_str = $url . "appid={$appId}&secret={$appSecret}&js_code={$code}&grant_type=authorization_code";
            $html = file_get_contents($url_str);
            $res = json_decode($html, true);
            //链接数据库
            $dbhost = 'localhost';  // mysql服务器主机地址
            $dbuser = 'root';            // mysql用户名
            $dbpass = 'hahacscs';          // mysql用户名密码
            $conn = mysqli_connect($dbhost, $dbuser, $dbpass);
            if (!$conn) {
                die('连接失败: ' . mysqli_error($conn));
            }
            mysqli_select_db($conn, 'haoyayitiku');
            $data = array();
            $sql_str = "select * from haoyayitiku_user_info where openid = '{$res['openid']}';";
            $result = mysqli_query($conn, $sql_str);
            $queryNum = mysqli_num_rows($result);
            if ($result > 0 && $queryNum == 0) {
                $sql_insert = "insert into haoyayitiku_user_info (openid,session_key,is_authorize,wei_nickName,wei_gender,wei_city,wei_province,wei_country"
                        . ",wei_avatarUrl) values ('{$res['openid']}','{$res['session_key']}',1,'$w_name',$w_gender,'$w_city','$w_province','$w_country','$w_avatarUrl')";
                $retval = mysqli_query($conn, $sql_insert);
                if (!$retval) {
                    $ret = array(
                        'status' => 0,
                        'msg' => '数据插入失败'
                    );
                }
                $result = mysqli_query($conn, $sql_str);
                $row = mysqli_fetch_assoc($result);
                $ret = array(
                    'status' => 1,
                    'msg' => '数据插入成功',
                    'data' => array(
                        "trd_session_key" => "{$row['id']}.@.{$res['openid']}"
                    )
                );
            } elseif ($queryNum == 1) {
                $sql_update = "update haoyayitiku_user_info set session_key = '{$res['session_key']}',wei_nickName = '$w_name',wei_gender='$w_gender',"
                        . "wei_city='$w_city',wei_province='$w_province',wei_country='$w_country',wei_avatarUrl='$w_avatarUrl' where openid = '{$res['openid']}'";
                $retval = mysqli_query($conn, $sql_update);
                if (!$retval) {
                    $ret = array(
                        'status' => 0,
                        'msg' => '数据更新失败'
                    );
                }
                $row = mysqli_fetch_assoc($result);
                $ret = array(
                    'status' => 1,
                    'msg' => '数据更新成功',
                    'data' => array(
                        "trd_session_key" => "{$row['id']}.@.{$res['openid']}"
                    )
                );
            }
            //关闭数据库
            mysqli_close($conn);
        } else {
            $ret = array(
                'status' => 0,
                'msg' => '失败'
            );
        }

        $ret = json_encode($ret);
        echo $ret;
        return;
        //return 0;
    }

}
